/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2021-11-01 22:13:43
 * @LastEditors: sueRimn
 * @LastEditTime: 2021-11-01 23:41:21
 */
const appName = "EIS",
  package = getPackageName(appName),
  userNameInput = () => id("et_user_name").findOne(), //用户名输入框
  pwdInput = () => id("et_password").findOne(), //密码输入框
  submit = () => id("bt_submit_button").findOne(), //登录按钮
  push = {
    token: "ddd86949e9044ba7bbb3fc6f6871ae8f",
    url: "https://open.feishu.cn/open-apis/bot/v2/hook/6a037af9-d04b-4c67-bb96-ec3302325208",
  },
  user = {
    userName: "52715",a
    passWord: "trxn@111",
    screenPwd: "910423",
  };
//判断是否在闹钟页面

unlock();
//判断程序是否已打开，如果为了避免定位出现问题已打开先退出
if (currentPackage() === package) {
  exitApp();
}

launchApp(appName);
console.log("打开应用:" + appName); //打开Tasker app
sleep(10000);
//登录
login();

//点击打卡app
id("rl_punch").findOne().click();
console.log("打开打卡app");
//打卡完成
daKa();
var msg = pushMessage(
  "eis打卡成功",
  "恭喜张好佳同学打卡成功，打卡时间：" +
    dateFtt("yyyy-MM-dd hh:mm:ss", new Date())
);
console.log(msg);
toast("自动打卡成功");
//退出应用
exitApp();

// 输入密码
function password_input() {
  for (var i = 0; i < user.screenPwd.length; i++) {
    var p = text(user.screenPwd[i]).findOne().bounds();
    click(p.centerX(), p.centerY());
    sleep(100);
  }
}

// 解锁屏幕
function unlock() {
  if (!device.isScreenOn()) {
    device.wakeUp();
    sleep(500);
    swipe(500, 2000, 500, 1000, 210);
    password_input();
    console.log("屏幕解锁成功");
  }
}

//登录
function login() {
  if (id("rl_punch").find().size() <= 0) {
    console.log("userName", userNameInput().setText(user.userName));
    sleep(1000);
    console.log("passWord", pwdInput().setText(user.passWord));
    sleep(1000);
    console.log("submit", submit().click());
    sleep(5000);
  }
  //校验打卡是否成功
  if (id("rl_punch").find().size() <= 0) {
    pushMessage(
      "eis登录失败",
      "张好佳同学打卡失败，打卡时间：" +
        dateFtt("yyyy-MM-dd hh:mm:ss", new Date())
    );
    console.log("登录失败");
    return;
  } else {
    console.log("登录成功");
  }
}

//打卡函数
function daKa() {
  sleep(2000);
  id("iv_sign_in").findOne().click();
  console.log("打卡成功");
  sleep(3000);
}

function pushMessage(title, content) {
  return http.postJson(push.url, {
    "msg_type": "post",
    "content": {
      "post": {
        "zh_cn": {
          "title": title,
          "content": [
            [{
                "tag": "text",
                "text": content
              },
              {
                "tag": "at",
                "user_id": "all"
              }
            ]
          ]
        }
      }
    }
  });
}

//退出程序
function exitApp() {
  back();
  if (currentPackage() === package) {
    //判断是否在应用内,如果在则再次返回
    sleep(200);
    exitApp();
    return;
  }
  console.log("退出应用");
  // 未设置自动计时时退出脚本
}

/**************************************时间格式化处理************************************/
function dateFtt(fmt, date) {
  //author: meizz
  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    S: date.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
}